/**
 * Created by Administrator on 2016/9/10.
 */

var chat = {
    'face_img_uri':'/static/m/images/QQexpression/',
    "online":function(data){
        var error = data.error;
        var msg = data.msg;
        //alert(msg);
    },
    "tranformation_expresson":function(msg){
        //匹配表情
        var msgVal = msg;
        var msgInputValArr = msgVal.match(/\[\d+\]/g);
        var marrlen = 0;
        if(msgInputValArr != null){
            marrlen = msgInputValArr.length;
            if(marrlen>0){
                for(var m=0; m<marrlen; m++){
                    var picnum = msgInputValArr[m].substring(1,msgInputValArr[m].length-1);
                    var picurl = "<img src='"+chat.face_img_uri+picnum+".gif' class='d-face'>";
                    msgVal = msgVal.replace("["+picnum+"]",picurl);
                }
            }
        }
        return msgVal;
    },
    "show_time":function(data){
        //to uid,和谁聊天
        var to_uid = data.to_uid;
        var msg_time_key = "msg_time_"+to_uid;
        var msg_time = jq.cookie(msg_time_key);
        var time = data.time;
        var pre_date = new Date(time*1000); //毫秒
        var show_time = pre_date.getHours()+":"+pre_date.getMinutes()+":"+pre_date.getSeconds();
        var show_time_str = "今天&nbsp;"+show_time;
        var return_msg = {"show":1,"show_str":show_time_str};

        if(typeof(msg_time) == "undefined"){
            jq.cookie(msg_time_key,time);
            return_msg.show = 1;
        }else{
            var msg_time_interval = 300;

            if(( time - msg_time ) >= msg_time_interval){  //秒
                jq.cookie(msg_time_key,time);
                return_msg.show = 1;
            }

        }

        return return_msg;
    },
    "get_msg_showtime":function(data){
        //to uid,和谁聊天
        var to_uid = data.from_uid;
        var msg_time_key = "msg_time_"+to_uid;
        var msg_time = jq.cookie(msg_time_key);
        var time = data.time;
        var pre_date = new Date(time*1000); //毫秒
        var D=['00','01','02','03','04','05','06','07','08','09'];
        var show_time = pre_date.getHours()+":"+(D[pre_date.getMinutes()]||pre_date.getMinutes())+":"+(D[pre_date.getSeconds()]||pre_date.getSeconds());
        var show_time_str = "今天 "+show_time;
        var return_msg = {"show":0,"show_str":show_time_str};

        if(typeof(msg_time) == "undefined"){
            jq.cookie(msg_time_key,time);
            return_msg.show = 1;
        }else{
            var msg_time_interval = 300;

            if(( time - msg_time ) >= msg_time_interval){  //秒
                jq.cookie(msg_time_key,time);
                return_msg.show = 1;
            }

        }

        return return_msg;
    },
    //收到消息
    "get_msg":function(data,ws){
        //获取消息

        var msg = data.msg;
        var error = data.error;
        var from_uid = parseInt(data.from_uid);
        var time = data.time;

        var show_msg = chat.tranformation_expresson(msg);
        var check_msg_time = chat.get_msg_showtime(data);

        jq("#links_map > A").each(function(event){

            var uid = parseInt(jq(this).attr('uid'));

            if(from_uid == uid){
                jq(this).find('P').html(show_msg);
                jq(this).find('.time').text(check_msg_time.show_str);
            }
        });

        //var other_msg = '<div class="msg-item item.senderself" msg-type=\'item.type\' msg-content=\'item.content\'>';
        //
        //if(check_msg_time.show == 1){
        //    other_msg+='<div class="mgs-time"><time>'+check_msg_time.show_str+'</time></div>';
        //}
        //
        //other_msg+='<a href="javascript:;"><img class="msg-user-img" src="./images/pic.jpg" alt="" /></a>';
        //other_msg+='<div class="msg-content"><div class="msg-content-inner">'+show_msg+'</div><div class="msg-content-arrow"></div></div>';
        //other_msg+='<div class="mui-item-clear"></div></div>';
        //
        //jq("#msg-list").append(other_msg).scrollTop(jq('#msg-list').height() + 300);

    }


};
